function match = findSortedMatch(features, pos, threfunc, matchfunc, varargin)
if ~isa(threfunc, 'function_handle')
    error('THREFUNC must be a handler function.');
end
if ~isa(matchfunc, 'function_handle')
    error('MATCHFUNC must be a handler function.');
end
num = length(features);

clear match;
count = 0;
for i = 1:num
    for j = i+1:num
        if threfunc(features{i}, features{j})
            break;
        end
        if matchfunc(features{i}, pos{i}, features{j}, pos{j}, varargin)
            count = count + 1;
            match{count} = [i, j];
        end
    end
end

end
